package com.penghk.earth.Controller;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class BaseController {

    public static final String EXCEL_CONTENT_TYPE = "application/vnd.ms-excel";

    @RequestMapping("/getExcel")
    public void exportExcelToClient(HttpServletResponse response) throws IOException {

        HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream("/home/penghk/下载/demo.xls"));
        response.setContentType(EXCEL_CONTENT_TYPE);
        response.setHeader("Content-Disposition", "attachment;filename=" + "demo.xls" + ";");
        OutputStream out = response.getOutputStream();
        out.flush();
        workBook.write(out);
        workBook.close();
        out.close();


    }

    @RequestMapping("/getFile")
    public void exportFileToClient(HttpServletResponse response) throws IOException {
        File file = new File("/home/penghk/下载/demo.xls");
        FileInputStream inputStream = new FileInputStream(file);
        response.setContentType(EXCEL_CONTENT_TYPE);
//        response.setHeader("Content-Disposition", "attachment;filename=" + "demo.xls" + ";");
        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len =inputStream.read(buf)) != -1){
            out.write(buf);
        }
        out.flush();
        out.close();
    }

    @RequestMapping("/getImage")
    public void exportImageToClient(HttpServletResponse response) throws IOException {
        File file = new File("/home/penghk/下载/demo.jpg");
        FileInputStream inputStream = new FileInputStream(file);
        response.setContentType("image/jpg");
//        response.setHeader("Content-Disposition", "attachment;filename=" + "demo.jpg" + ";");
        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len =inputStream.read(buf)) != -1){
            out.write(buf);
        }
        out.flush();
        out.close();
    }

    private String getEncodeFileName(String zipFileName, String charset) throws UnsupportedEncodingException {
        String encodeFileName = java.net.URLEncoder.encode(zipFileName, charset);
        encodeFileName = encodeFileName.replaceAll("\\+", "%20");
        return encodeFileName;
    }

}
